05/01/2020

Exploring Visualization capabilities of R

  • plot()
  • ggplot()
  • Bonus: magick()

Plot() Function

  • Basic function in R
  • 2D format

Generic syntax: plot(x,y…)
Advanced syntax: plot(x, y, type, main, sub, xlab, ylab)

main: text for the main title
sub: text for the sub-title (under x-axis)

type:
p”: points
l”: lines
b”: both point and lines in a single place
c”: join empty point by the lines
o”: both lines and over-plotted point
h”: histogram
s”: stair steps
n”: no plotting

xlab: x-axis legends
ylab: y-axis legends

Example Exercise Using Plot()

Exam grades of 10 students in two courses, X and Y, respectively


X = 40, 15, 50, 12, 22, 29, 21, 35, 14, 15
Y = 41, 42, 32, 14, 42, 27, 13, 50, 33, 22

Exercise 1: Define X and plot as lines plot

X = c(40, 15, 50, 12, 22, 29, 21, 35, 14, 15)
plot(X ,type = "l")

Exercise 2: Define Y and plot as points plot

Y = c(41, 42, 32, 14, 42, 27, 13, 50, 33, 22)
plot(Y ,type = "p")

Plot() Function Capabilities


Source: Kumar, 2020

Data source for Case Study

Package: nCov2019
By: Dr. Guangchuang Yu (Southern Medical University)

remotes::install_github("GuangchuangYu/nCov2019")  

Explore 1st Impression of dataset

Assign x and y

x <- get_nCov2019()
y <- load_nCov2019()

Check results for x and y

x
  
China (total confirmed cases): 95901
last update: 2020-12-21 20:45:32
y
  
nCov2019 historical data 
last update: 2020-11-26 

Summary of worldwide data

x['global']
    name           confirm   suspect dead    deadRate  showRate  heal
1   China          95901      7      4771    4.97      FALSE     89480
2   United States  18277433   0      324898  1.78      FALSE     10622096
3   India          10055560   0      145810  1.45      FALSE     9606111
4   Brazil         7238600    0      186764  2.58      FALSE     6409986
5   Russia         2850042    0      50723   1.78      FALSE     2273510
6   France         2529756    0      60665   2.4       FALSE     189638
7   United Kingdom 2079564    0      67718   3.26      FALSE     4380
8   Turkey         2043704    0      18351   0.9       FALSE     1834705
9   Italy          1964054    0      69214   3.52      FALSE     1281258
10  Spain          1817448    0      48926   2.69      FALSE     196958
11  Argentina      1541285    0      41813   2.71      FALSE     1368346
12  Germany         1531998   0      26655   1.74      FALSE     1129280

A Static heat map

ggplot2 Package

install.packages("ggplot2")
  • Powerful package to draw graphics
  • ‘gg’ stands for Grammar of Graphics
  • Works with dataframes and not individual vectors (unlike base graphics)

ggplot() Function Capabilities




Source: Holtz, 2020

A colorful line graph - Top 10 Countries

A gauge plot - Incubation Time


Source: Kanevsky, 2020

A bar chart - Spectrum of Illness Severity


Source: Kanevsky, 2020

A bar chart - Clinical Manifestations


Source: Kanevsky, 2020

A bar & line chart - Case Fatality Rate (CFR) by Age Groups


Source: Kanevsky, 2020

A bar chart with timeline - Period of Infectivity


Source: Kanevsky, 2020

Bonus! Animated maps using magick()

Animated growth of confirmed cases

install.packages("magick")  

References

EDUCBA. 2020. Plot Function In R. [online] Available at: https://www.educba.com/plot-function-in-r/.

Holtz, Y., 2020. Data Visualization With R And Ggplot2. [online] R-graph-gallery.com. Available at: https://www.r-graph-gallery.com/ggplot2-package.html.

Kanevsky, G., 2020. Facts About Coronavirus Disease 2019 (COVID-19) In 5 Charts Created With R And Ggplot2. [online] Novyden.blogspot.com. Available at: https://novyden.blogspot.com/2020/03/facts-about-coronavirus-disease-2019.html.

Kumar, P., 2020. Understanding Plot() Function In R. [online] JournalDev. Available at: https://www.journaldev.com/36083/plot-function-in-r.

Pedersen, T., 2020. Ggplot2 Package. [online] Rdocumentation.org. Available at: https://www.rdocumentation.org/packages/ggplot2/versions/3.3.2.

Qian, X., 2020. Visualize The Pandemic With R #COVID-19. [online] Medium. Available at: https://towardsdatascience.com/visualize-the-pandemic-with-r-covid-19-c3443de3b4e4.